﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Infrastructure.Interception;
using System.Linq;
using System.Text;
using System.Web;

namespace MVCEF.DBContext
{
    /// <summary>
    /// EF日志格式化
    /// </summary>
    public class CustomDatabaseLogFormatter : DatabaseLogFormatter
    {

        public CustomDatabaseLogFormatter(DbContext context, Action<string> writeAction)
            : base(context, writeAction)
        {

        }

        public override void LogCommand<TResult>(DbCommand command, DbCommandInterceptionContext<TResult> interceptionContext)
        {

        }

        public override void LogResult<TResult>(DbCommand command, DbCommandInterceptionContext<TResult> interceptionContext)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < command.Parameters.Count; i++)
            {
                sb.AppendLine(string.Format("参数名称：{0}，值：{1}", command.Parameters[0].ParameterName, command.Parameters[0].Value));
            }
            Write(command.CommandText + Environment.NewLine
                                      + command.CommandTimeout + Environment.NewLine
                                      + command.CommandType + Environment.NewLine
                                      + sb.ToString());
        }
    }
}